<?php

global $_GPC;
global $_W;
$shopid = check_right(2);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
if ($operation == 'display' || $operation == 'charge') {
    $userlist = pdo_fetchall('SELECT id,realname FROM ' . tablename('j_hmoney_user') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\' order by id desc');
    $userary = array();
    foreach ($userlist as $row) {
        $userary[$row['id']] = $row['realname'];
    }
    $alltable = pdo_fetchall('SELECT a.id,a.title,(b.title) as pcatetitle FROM ' . tablename('j_hmoney_table') . ' a left join ' . tablename('j_hmoney_tablecategory') . ' b on a.pcate=b.id  WHERE a.weid = \'' . $_W['uniacid'] . '\' and a.groupid=\'' . $shopid . '\' order by a.id desc');
    $tableAry = array();
    foreach ($alltable as $row) {
        $tableAry[$row['id']] = $row['pcatetitle'] . '<br>' . $row['title'];
    }
} else {
    if ($operation == 'salepage') {
        $pindex = max(1, intval($_GPC['page']));
        $psize = 10;
        $condition = ' and groupid =\'' . $shopid . '\' ';
        intval($_GPC['datetype']);
        switch (intval($_GPC['datetype'])) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
        }
        if ($_GPC['keyword']) {
            $condition .= ' and (out_trade_no=\'' . $_GPC['keyword'] . '\') ';
        }
        if (isset($_GPC['paytype'])) {
            $condition .= ' and paytype = \'' . $_GPC['paytype'] . '\' ';
        }
        if ($_GPC['memberno']) {
            $condition .= ' and memberno>0 ';
        }
        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' order by id desc  LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
        $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' ');
        $items = pdo_fetch('SELECT COUNT(*) as num1,sum(order_fee+teafee) as fee1,sum(discount_fee) as fee2,sum(paid_fee) as fee3,sum(teafee) as fee4,sum(num) as num2,sum(person) as num3 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 and status<4 ');
        $paytypeList1 = pdo_fetchall('SELECT paytype,COUNT(*) as num1,sum(paid_fee1) as fee1 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 and status<4 group by paytype ');
        $paytypeList2 = pdo_fetchall('SELECT paytype2,COUNT(*) as num1,sum(paid_fee2) as fee1 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 and paytype2>-1 and status<4 group by paytype2 ');
        $paytypeList = array();
        $temp = _getPaytpye('', $shopid);
        foreach ($paytypeList1 as $row) {
            $paytypeList[$row['paytype']] = array('str' => $temp[$row['paytype']], 'num' => $row['num1'], 'fee1' => $row['fee1']);
        }
        foreach ($paytypeList2 as $row) {
            $paytypeList[$row['paytype2']]['num'] = $paytypeList[$row['paytype2']]['num'] + $row['num1'];
            $paytypeList[$row['paytype2']]['fee1'] = $paytypeList[$row['paytype2']]['fee1'] + $row['fee1'];
        }
        $refundorder = pdo_fetch('SELECT COUNT(*) as num1,sum(num) as num2,sum(fee) as fee1 FROM ' . tablename('j_hmoney_refund') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and rtype=1 ');
        $refundorder['num2'] = $refundorder['num2'] == NULL ? 0 : $refundorder['num2'];
        $refundorder['fee1'] = $refundorder['fee1'] == NULL ? 0 : $refundorder['fee1'];
        $refundgoods = pdo_fetch('SELECT sum(num) as num1,sum(fee) as fee1 FROM ' . tablename('j_hmoney_refund') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and rtype=2 ');
        $refundgoods['num1'] = $refundgoods['num1'] == NULL ? 0 : $refundgoods['num1'];
        $refundgoods['fee1'] = $refundgoods['fee1'] == NULL ? 0 : $refundgoods['fee1'];
        $refundary = pdo_fetchall('SELECT paytype,sum(fee) as fee1 FROM ' . tablename('j_hmoney_refund') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' and  status=1 group by paytype ');
        $refundlist = array();
        foreach ($refundary as $row) {
            if (!isset($paytypeList[$row['paytype']])) {
                $paytypeList[$row['paytype']] = array('str' => $temp[$row['paytype']], 'num' => 0, 'fee1' => 0);
            }
            $paytypeList[$row['paytype']]['fee1'] = $paytypeList[$row['paytype']]['fee1'] - $row['fee1'];
        }
        exit(json_encode(array('success' => true, 'list' => $list, 'pageall' => $pageall, 'total' => $total, 'sumfee' => $items, 'refundorder' => $refundorder, 'refundgoods' => $refundgoods, 'paylist' => $paytypeList)));
    } else {
        if ($operation == 'getorderdetail') {
            $id = intval($_GPC['id']);
            $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and orderid=\'' . $id . '\' order by id asc');
            exit(json_encode(array('success' => true, 'list' => $list)));
        } else {
            if ($operation == 'getcharge') {
                $pindex = max(1, intval($_GPC['page']));
                $psize = 10;
                $condition = ' and a.groupid =\'' . $shopid . '\' ';
                intval($_GPC['datetype']);
                switch (intval($_GPC['datetype'])) {
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                }
                if ($_GPC['keyword']) {
                    $mid = pdo_fetchcolumn('SELECT id FROM ' . tablename('j_hmoney_membercard') . '  WHERE weid=\'' . $_W['uniacid'] . '\' and cardno=\'' . $_GPC['keyword'] . '\' limit 1');
                    $condition .= ' and (a.cid=\'' . $mid . '\') ';
                }
                if (isset($_GPC['paytype'])) {
                    $condition .= ' and paytype = \'' . $_GPC['paytype'] . '\' ';
                }
                $sql = 'SELECT a.*,b.cardno,b.realname FROM ' . tablename('j_hmoney_recharge') . ' a left join ' . tablename('j_hmoney_membercard') . ' b on a.cid=b.id WHERE a.weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' order by a.id desc  LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
                $list = pdo_fetchall($sql);
                $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('j_hmoney_recharge') . ' a WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' ');
                if ($pindex == 1) {
                    $condition = str_replace('a.', '', $condition);
                    $chargeitems = pdo_fetch('SELECT COUNT(*) as num1,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and usetype=0 ');
                    $chargesale = pdo_fetch('SELECT COUNT(*) as num1,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and usetype=1 ');
                    $chargeback = pdo_fetch('SELECT COUNT(*) as num1,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and usetype=2 ');
                    $chargecount = pdo_fetchall('SELECT paytype,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' and usetype=0 and  status=1 group by paytype');
                    $chargelist = array();
                    foreach ($chargecount as $row) {
                        $chargelist[$row['paytype']] = $row['fee1'];
                    }
                    $membercount = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid=\'' . $_W['uniacid'] . '\'  and createtime>=\'' . $user['lasttime'] . '\' and createtime<=\'' . TIMESTAMP . '\' ');
                    $memberallcount = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid=\'' . $_W['uniacid'] . '\'  ');
                }
                exit(json_encode(array('success' => true, 'list' => $list, 'total' => $total, 'chargeitems' => $chargeitems, 'chargesale' => $chargesale, 'chargeback' => $chargeback, 'chargelist' => $chargelist, 'membercount' => $membercount, 'memberallcount' => $memberallcount)));
            } else {
                if ($operation == 'outputs') {
                    $userlist = pdo_fetchall('SELECT id,realname FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\' order by id desc');
                    $userary = array();
                    foreach ($userlist as $row) {
                        $userary[$row['id']] = $row['realname'];
                    }
                    $condition = ' and groupid =\'' . $shopid . '\' ';
                    intval($_GPC['datetype']);
                    switch (intval($_GPC['datetype'])) {
                        case 1:
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                        case 4:
                            break;
                    }
                    if ($_GPC['keyword']) {
                        $condition .= ' and (out_trade_no=\'' . $_GPC['keyword'] . '\') ';
                    }
                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' order by id desc ');
                    require_once '../framework/library/phpexcel/PHPExcel.php';
                    $objPHPExcel = new PHPExcel();
                    $objPHPExcel->getProperties()->setCreator('智慧收银台')->setLastModifiedBy('智慧收银台')->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('备份数据')->setKeywords('excel')->setCategory('result file');
                    $excelAry = array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'X', 19 => 'T', 20 => 'U', 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z');
                    $headerAry = array(0 => '编号', 1 => '单号', 2 => '消费对象', 3 => '数量', 4 => '总金额', 5 => '茶位', 6 => '优惠', 7 => '实付', 8 => '支付A', 9 => '支付A金额', 10 => '支付B', 11 => '支付B金额', 12 => '收银员', 13 => '时间', 14 => '状态', 15 => '备注', 16 => '店铺');
                    $i = 0;
                    while ($i < count($headerAry)) {
                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($excelAry[$i] . '1', $headerAry[$i]);
                        $i = $i + 1;
                    }
                    $statusAry = array(0 => '空闲', 1 => '开台', 2 => '完成');
                    $num = 2;
                    foreach ($list as $row) {
                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $num, '\'' . $row['out_trade_no'])->setCellValue('B' . $num, '\'' . $row['serialnum'])->setCellValue('C' . $num, intval($row['memberno']) ? '会员' : '散客')->setCellValue('D' . $num, $row['num'])->setCellValue('E' . $num, $this->_2money($row['order_fee'] + $row['teafee']))->setCellValue('F' . $num, $this->_2money($row['teafee']))->setCellValue('G' . $num, $this->_2money($row['total_fee']))->setCellValue('H' . $num, $this->_2money($row['discount_fee']))->setCellValue('I' . $num, $row['ispay'] ? _getPaytpye($row['paytype'], $shopid) : '')->setCellValue('J' . $num, $this->_2money($row['paid_fee1']))->setCellValue('K' . $num, $row['ispay'] ? _getPaytpye($row['paytype2'], $shopid) : '')->setCellValue('L' . $num, $this->_2money($row['paid_fee2']))->setCellValue('M' . $num, $userary[$row['userid']])->setCellValue('N' . $num, date('Y/m/d H:i', $row['createtime']))->setCellValue('O' . $num, $statusAry[$row['status']])->setCellValue('P' . $num, $row['remark'])->setCellValue('Q' . $num, $shop['companyname']);
                        $num = $num + 1;
                    }
                    $objPHPExcel->getActiveSheet()->setTitle('销售记录');
                    $objPHPExcel->setActiveSheetIndex(0);
                    ob_end_clean();
                    header('Content-Type: application/vnd.ms-excel');
                    header('Content-Disposition:attachment;filename="sale_temp.xls"');
                    header('Cache-Control:max-age=0');
                    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                    $objWriter->save('php://output');
                    exit(0);
                } else {
                    if ($operation == 'outputs_detail') {
                        $userlist = pdo_fetchall('SELECT id,realname FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\' order by id desc');
                        $userary = array();
                        foreach ($userlist as $row) {
                            $userary[$row['id']] = $row['realname'];
                        }
                        $condition = ' and groupid =\'' . $shopid . '\' ';
                        intval($_GPC['datetype']);
                        switch (intval($_GPC['datetype'])) {
                            case 1:
                                break;
                            case 2:
                                break;
                            case 3:
                                break;
                            case 4:
                                break;
                        }
                        if ($_GPC['keyword']) {
                            $condition .= ' and (out_trade_no=\'' . $_GPC['keyword'] . '\') ';
                        }
                        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid in( SELECT id FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' ) order by id asc');
                        require_once '../framework/library/phpexcel/PHPExcel.php';
                        $objPHPExcel = new PHPExcel();
                        $objPHPExcel->getProperties()->setCreator('智慧收银台')->setLastModifiedBy('智慧收银台')->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('备份数据')->setKeywords('excel')->setCategory('result file');
                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '单号')->setCellValue('B1', '条码')->setCellValue('C1', '品名')->setCellValue('D1', '原价')->setCellValue('E1', '销售价格')->setCellValue('F1', '数量')->setCellValue('G1', '金额')->setCellValue('H1', '属性')->setCellValue('I1', '支付状态')->setCellValue('J1', '状态')->setCellValue('K1', '时间')->setCellValue('L1', '店铺');
                        $statusary = array(0 => '', 1 => '下单', 2 => '完成');
                        $orderGoodsPay = array(0 => '未付款', 1 => '已付款', 2 => '', 3 => '部分退款', 4 => '全部退款');
                        $num = 2;
                        foreach ($list as $row) {
                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $num, '\'' . $row['out_trade_no'])->setCellValue('B' . $num, '\'' . $row['goodsn'])->setCellValue('C' . $num, $row['title'])->setCellValue('D' . $num, $row['oldprice'])->setCellValue('E' . $num, $row['price'])->setCellValue('F' . $num, $row['num'])->setCellValue('G' . $num, $this->_2money($row['total_fee']))->setCellValue('H' . $num, $row['taste'])->setCellValue('I' . $num, $orderGoodsPay[$row['ispay']])->setCellValue('J' . $num, $statusary[$row['status']])->setCellValue('K' . $num, date('Y/m/d H:i', $row['createtime']))->setCellValue('L' . $num, $shop['companyname']);
                            $num = $num + 1;
                        }
                        $objPHPExcel->getActiveSheet()->setTitle('商品销售记录');
                        $objPHPExcel->setActiveSheetIndex(0);
                        ob_end_clean();
                        header('Content-Type: application/vnd.ms-excel');
                        header('Content-Disposition:attachment;filename="salegoods_temp.xls"');
                        header('Cache-Control:max-age=0');
                        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                        $objWriter->save('php://output');
                        exit(0);
                    } else {
                        if ($operation == 'outputc') {
                            $userlist = pdo_fetchall('SELECT id,realname FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\' order by id desc');
                            $userary = array();
                            foreach ($userlist as $row) {
                                $userary[$row['id']] = $row['realname'];
                            }
                            $condition = ' and a.groupid =\'' . $shopid . '\' ';
                            intval($_GPC['datetype']);
                            switch (intval($_GPC['datetype'])) {
                                case 1:
                                    break;
                                case 2:
                                    break;
                                case 4:
                                    break;
                            }
                            if ($_GPC['keyword']) {
                                $mid = pdo_fetchcolumn('SELECT id FROM ' . tablename('j_hmoney_membercard') . '  WHERE weid=\'' . $_W['uniacid'] . '\' and cardno=\'' . $_GPC['keyword'] . '\' limit 1');
                                $condition .= ' and (a.cid=\'' . $mid . '\') ';
                            }
                            $sql = 'SELECT a.*,b.cardno,b.realname FROM ' . tablename('j_hmoney_recharge') . ' a left join ' . tablename('j_hmoney_membercard') . ' b on a.cid=b.id WHERE a.weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' order by a.id desc  ';
                            $list = pdo_fetchall($sql);
                            require_once '../framework/library/phpexcel/PHPExcel.php';
                            $objPHPExcel = new PHPExcel();
                            $objPHPExcel->getProperties()->setCreator('智慧收银台')->setLastModifiedBy('智慧收银台')->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('备份数据')->setKeywords('excel')->setCategory('result file');
                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '卡号')->setCellValue('B1', '操作')->setCellValue('C1', '支付方式')->setCellValue('D1', '操作人')->setCellValue('E1', '操作金额')->setCellValue('F1', '赠送金额')->setCellValue('G1', '变动前金额')->setCellValue('H1', '变动后金额')->setCellValue('I1', '时间')->setCellValue('J1', '状态')->setCellValue('K1', '备注')->setCellValue('L1', '店铺');
                            $doType = array(0 => '充值', 1 => '消费', 2 => '退还');
                            $paytype = array(0 => '微信', 1 => '支付宝', 2 => '现金', 3 => '银行卡', -1 => '-');
                            $num = 2;
                            foreach ($list as $row) {
                                $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $num, '\'' . $row['cardno'])->setCellValue('B' . $num, $doType[$row['usetype']])->setCellValue('C' . $num, $paytype[$row['paytype']])->setCellValue('D' . $num, $userary[$row['userid']] ? $userary[$row['userid']] : '后台')->setCellValue('E' . $num, $this->_2money($row['cash']))->setCellValue('F' . $num, $this->_2money($row['gift']))->setCellValue('G' . $num, $this->_2money($row['startfee']))->setCellValue('H' . $num, $this->_2money($row['endfee']))->setCellValue('I' . $num, date('Y/m/d H:i', $row['createtime']))->setCellValue('J' . $num, $row['status'] ? '成功' : '失败')->setCellValue('K' . $num, $row['remark'])->setCellValue('L' . $num, $shop['companyname']);
                                $num = $num + 1;
                            }
                            $objPHPExcel->getActiveSheet()->setTitle('充值记录');
                            $objPHPExcel->setActiveSheetIndex(0);
                            ob_end_clean();
                            header('Content-Type: application/vnd.ms-excel');
                            header('Content-Disposition:attachment;filename="charge_temp.xls"');
                            header('Cache-Control:max-age=0');
                            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                            $objWriter->save('php://output');
                            exit(0);
                        }
                    }
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/sale');
if (null == 3) {
    $condition .= ' and a.createdate>=\'' . date('Y-m-d', strtotime('-6 day')) . '\' ';
} else {
}